package com.xiaoma.tpo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.BonusSignAchievementDao;
import com.xiaoma.tpo.dao.BonusSignRuleDao;
import com.xiaoma.tpo.dao.BonusTaskDao;
import com.xiaoma.tpo.dao.BonusTaskRecordDao;
import com.xiaoma.tpo.dao.ClsAchievementBonusRecordDao;
import com.xiaoma.tpo.dao.ClsAchievementResourceDao;
import com.xiaoma.tpo.dao.UserSignDao;
import com.xiaoma.tpo.domain.BonusClassAchievementRecord;
import com.xiaoma.tpo.domain.BonusClassAchievementResource;
import com.xiaoma.tpo.domain.BonusSignAchievement;
import com.xiaoma.tpo.domain.BonusSignRule;
import com.xiaoma.tpo.domain.BonusTask;
import com.xiaoma.tpo.domain.BonusTaskRecord;
import com.xiaoma.tpo.domain.BonusTaskRule;
import com.xiaoma.tpo.domain.BonusUserSign;
import com.xiaoma.tpo.dto.BonusBoard;
import com.xiaoma.tpo.dto.BonusClassAchievementRecordVo;
import com.xiaoma.tpo.resource.TPOConstants;

@Service
public class BonusPointService {

    @Autowired
    private UserSignDao userSignDao;

    @Autowired
    private BonusSignRuleDao signRuleDao;

    @Autowired
    private BonusTaskDao bonusTaskDao;

    @Autowired
    private BonusSignAchievementDao bonusSignAchievementDao;

    @Autowired
    private ClsAchievementBonusRecordDao clsAchievementBonusRecordDao;

    @Autowired
    private ClsAchievementResourceDao clsAchievementResourceDao;
    
    @Autowired
    private BonusTaskRecordDao bonusTaskRecordDao;

    public void save(BonusUserSign bonusUserSign) {
        userSignDao.insert(bonusUserSign);
    }

    public void save(BonusSignAchievement bonusSignAchievement) {
        bonusSignAchievementDao.insert(bonusSignAchievement);
    }

    public List<BonusSignAchievement> findSignAchievements(BonusSignAchievement entity) {
        return bonusSignAchievementDao.findByColumns(entity);
    }

    public void update(BonusSignAchievement entity) {
        bonusSignAchievementDao.update(entity);
    }

    public BonusUserSign findLastSign(int userId) {
        return userSignDao.findLastSign(userId);
    }

    public void saveAchievementBonusRecord(BonusClassAchievementRecord bonusClassAchievementRecord) {
        clsAchievementBonusRecordDao.insert(bonusClassAchievementRecord);
    }

    public BonusClassAchievementRecord findAchievementBonusRecord(int userId, int classId, int achievement) {
        BonusClassAchievementRecord entity = new BonusClassAchievementRecord();
        entity.setClsClassId(classId);
        entity.setAchievement(achievement);
        entity.setUserId(userId);
        List<BonusClassAchievementRecord> records = clsAchievementBonusRecordDao.findByColumns(entity);
        return records.size() > 0 ? records.get(0) : null;
    }

//    public List<BonusClassAchievementRecordVo> findAchievementByUserId(int userId) {
//        return clsAchievementBonusRecordDao.findAchievementBonusRecordVos(userId);
//    }

    public BonusClassAchievementResource findAchievementResource(int classId, int achievement) {
        BonusClassAchievementResource entity = new BonusClassAchievementResource();
        entity.setAchievement(achievement);
        entity.setClsClassId(classId);
        List<BonusClassAchievementResource> resources = clsAchievementResourceDao.findByColumns(entity);
        return resources.size() > 0 ? resources.get(0) : null;
    }

    public List<BonusTaskRule> findRulesRandom(int timeType, int count) {
        return bonusTaskDao.findRulesRandom(timeType, count);
    }
    
    public List<BonusTaskRule> findRules(int timeType) {
        return bonusTaskDao.findRules(timeType);
    }

    public List<BonusTask> getTasksByType(int timeType) {
        return bonusTaskDao.findTaskByType(timeType);
    }

    public BonusTask getFixedTasks(int taskRuleId) {
        BonusTask entity = new BonusTask();
        entity.setTimeType(TPOConstants.TASK_FIXED);
        entity.setTaskRuleId(taskRuleId);
        List<BonusTask> tasks = bonusTaskDao.findByColumns(entity);
        return tasks.size() > 0 ? tasks.get(0) : null;
    }

    public BonusTask findTaskById(int id) {
        return bonusTaskDao.getById(id);
    }

    public int saveBonusTask(BonusTask bonusTask) {
        return bonusTaskDao.insert(bonusTask);
    }

    public Map<String, Object> getSignPoint(int signCount) {
        List<BonusSignRule> rules = signRuleDao.findAll();
        Map<String, Object> result = new HashMap<String, Object>();
        for (int i = 0; i < rules.size(); i++) {
            if (signCount < rules.get(i).getSignCount()) {
                result.put("point", rules.get(i).getBonusPoint());
                result.put("bonusSignRule", rules.get(i - 1));
                break;
            } else if (signCount == rules.get(i).getSignCount()) {
                result.put("point", rules.get(i).getBonusPoint() + rules.get(i).getExtraPoint());
                result.put("bonusSignRule", rules.get(i));
                break;
            } else {
                if (i == rules.size() - 1) {
                    result.put("point", rules.get(i).getBonusPoint());
                    result.put("bonusSignRule", rules.get(i));
                }
            }
        }
        result.put("rules", rules);
        return result;
    }
    
    public List<BonusBoard> findUserSignAchievements(int userId) {
        return bonusSignAchievementDao.findUserSignAchievements(userId);
    }
    
    public List<BonusBoard> findClassAchievements(int userId) {
        return clsAchievementBonusRecordDao.findMaxAchievements(userId);
    }
    
    public List<BonusBoard> findTaskAchievements(int userId) {
        return bonusTaskRecordDao.findTaskAchievements(userId);
    }
    
    
    public void save(BonusTaskRecord bonusTaskRecord) {
        bonusTaskRecordDao.insert(bonusTaskRecord);
    }
    
    public List<BonusTaskRecord> findByColumn(BonusTaskRecord entity) {
        return bonusTaskRecordDao.findByColumns(entity);
    }
    

}
